package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TmcCommunityActivity;
import com.ruoyi.system.domain.TmcTopBanner;
import com.ruoyi.system.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@RestController
@RequestMapping("/system/banner")
public class BannerController extends BaseController {
    @Autowired
    private BannerService bannerService;

    //查询首页轮播图
    @GetMapping("/queryBannerList")
    public TableDataInfo queryBannerList(TmcTopBanner tmcTopBanner) {
        startPage();
        List<TmcTopBanner> list = bannerService.queryBannerList(tmcTopBanner);
        return getDataTable(list);
    }

    //查询小程序轮播图
    @GetMapping("/queryBanner")
    public TableDataInfo queryBanner() {
        return getDataTable(bannerService.queryBanner());
    }

    //查询首页轮播图
    @GetMapping("/queryBannerById")
    public AjaxResult queryBannerById(Long id) {
        return AjaxResult.success(bannerService.queryBannerById(id));
    }

    /**
     * 新增轮播图
     */
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmcTopBanner tmcTopBanner)
    {
        return  AjaxResult.success("新增成功",bannerService.insertTmcTopBanner(tmcTopBanner));
    }

    // 上传图片
    @Log(title = "图片", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload")
    @CrossOrigin
    public AjaxResult pictureUpload(MultipartFile file, Long id) {
        try {
            String profile = RuoYiConfig.getProfile();
            byte[] bytes = file.getBytes();
            //文件夹
            Path path2 = Paths.get(profile+"//banner//" + id);
            //具体文件
            Path path = Paths.get(profile+"//banner//" + id+ "//" + file.getOriginalFilename());
            Path path1 = Paths.get("profile//banner//" + id + "//" + file.getOriginalFilename());
            File file1 = path.toFile();
            //创建文件夹
            if (!file1.exists()) {
                path2.toFile().mkdirs();
            }
            if (file1.isFile()) {
                String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                String replace = path1.toString().replace(suffixName, "(" + (int) (Math.random() * 10) + ")" + suffixName);
            }
            Files.write(path, bytes);//创建文件
            TmcTopBanner tmcTopBanner = new TmcTopBanner();
            tmcTopBanner.setImageurl(path1.toString());
            tmcTopBanner.setId(id);
            if (bannerService.updateTmcTopBanner(tmcTopBanner) > 0) {
                return AjaxResult.success("文件上传成功");
            } else {
                if (file1.isFile()) {
                    file1.delete();
                }
                return AjaxResult.error("文件上传失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("文件上传失败");
        }
    }

    /**
     * 修改轮播图
     */
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmcTopBanner tmcTopBanner)
    {
        return toAjax(bannerService.updateTmcTopBanner(tmcTopBanner));
    }

    /**
     * 删除轮播图
     */
    @Log(title = "轮播图", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bannerService.deleteTmcTopBannerByIds(ids));
    }

}